package rsj.service.test;

import com.alibaba.fastjson.JSONObject;
import com.rlyy.aibase.db.Dbink;
import common.exec.RsjIllegalArgueException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rsj.api.test.entity.AddTestDataParam;
import rsj.db.PostgresqlConn;
import common.util.QueryRunnerFactory;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 数据录入
 *
 * @author yuanqinglong
 * @since 2021/3/15 13:35
 */
public interface IAddDataTemplateMethod {

    Logger logger = LoggerFactory.getLogger(IAddDataTemplateMethod.class);

    /**
     * 执行添加数据录入
     *
     * @param addTestDataParam 录入数据参数
     */
    default void executeAddingData(AddTestDataParam addTestDataParam) {
        Connection connection = PostgresqlConn.getConnection();
        String sql = this.generateAdditionStatement(addTestDataParam);
        try {
            int update = QueryRunnerFactory.getQueryRunner().update(connection, sql);
        } catch (SQLException e) {
            logger.error("添加测试数据失败、SQL:{}, 参数:{}", sql, JSONObject.toJSONString(addTestDataParam));
            throw new RsjIllegalArgueException("添加失败");
        } finally {
            Dbink.close(connection);
        }

    }


    /**
     * 添加语句SQL
     *
     * @param addTestDataParam 参数
     * @return SQL
     */
    String generateAdditionStatement(AddTestDataParam addTestDataParam);


}
